package com.forlove.app.qselect;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.forlove.app.R;

/**
 * Created by yjl on 2017/10/10.
 */

public class MainTabView extends RelativeLayout implements View.OnClickListener {

    private final static int POSITION_INDEX = 0; //首页
    //    private final static int POSITION_HOTLIST = 1;//达人榜
    private final static int POSITION_FRIEND = 1;//我的好友列表
    private View mRootView;
    private Context mContext;
    private MainTabClickListener mainTabClickListener;

    private RelativeLayout tab_index;//首页
    private RelativeLayout tab_hotlist;//我的好友
    private ImageView imageIndex;
    private TextView textIndex;
    private ImageView imageFriend;
    private TextView textFriend;
    private TextView view_has_circle_info; //有新的被圈信息提示
    private TextView view_has_new_friend;//有新的好友添加信息
    private TextView view_has_unread; //未读被圈消息

    public MainTabView(Context context) {
        super(context);
        initView(context);
    }

    public void setMainTabClickListener(MainTabClickListener mainTabClickListener) {
        this.mainTabClickListener = mainTabClickListener;
    }


    public MainTabView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MainTabView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        this.mContext = context;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mRootView = inflater.inflate(R.layout.layout_main_tab, this);
        tab_index = mRootView.findViewById(R.id.tab_index);
        tab_hotlist = mRootView.findViewById(R.id.tab_hotlist);
        tab_index.setOnClickListener(this);
        tab_hotlist.setOnClickListener(this);
        imageIndex = (ImageView) mRootView.findViewById(R.id.img_tab1);
        textIndex = (TextView) mRootView.findViewById(R.id.tv_tab1);

        imageFriend = (ImageView) mRootView.findViewById(R.id.img_tab2);
        textFriend = (TextView) mRootView.findViewById(R.id.tv_tab2);
        view_has_circle_info = mRootView.findViewById(R.id.view_has_circle_info);
        view_has_new_friend = mRootView.findViewById(R.id.view_has_new_friend);
        view_has_unread = mRootView.findViewById(R.id.view_has_unread);
    }


    public interface MainTabClickListener {
        public void OnTabSelected(int pos);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tab_index:
                showIndex();
                if (mainTabClickListener != null) {
                    mainTabClickListener.OnTabSelected(POSITION_INDEX);
                }
                break;
            case R.id.tab_hotlist:
                showFriend();
                if (mainTabClickListener != null) {
                    mainTabClickListener.OnTabSelected(POSITION_FRIEND);
                }
                break;
        }
    }

    public void showIndex() {
        setIndexSel();
        setFriendNor();
    }

    public void showFriend() {
        setIndexNor();
        setFriendSel();
    }

    private void setIndexSel() {
        imageIndex.setBackgroundResource(R.mipmap.tab_home_sel);
        textIndex.setTextColor(getResources().getColor(R.color.common_green));
    }

    private void setIndexNor() {
        imageIndex.setBackgroundResource(R.mipmap.tab_home_nor);
        textIndex.setTextColor(getResources().getColor(R.color.str_color_gray));
    }

    private void setFriendSel() {
        imageFriend.setBackgroundResource(R.mipmap.tab_friends_sel);
        textFriend.setTextColor(getResources().getColor(R.color.common_green));
    }

    private void setFriendNor() {
        imageFriend.setBackgroundResource(R.mipmap.tab_friends_nor);
        textFriend.setTextColor(getResources().getColor(R.color.str_color_gray));
    }


    public void showHasNewCircle() {
        view_has_unread.setVisibility(INVISIBLE);
        view_has_circle_info.setVisibility(VISIBLE);
    }


    public void showHasUnRead(int unread) {
        if (unread > 99) {
            view_has_unread.setText(99 + "+");
        } else {
            view_has_unread.setText(unread + "");
        }
        view_has_unread.setVisibility(VISIBLE);
        view_has_circle_info.setVisibility(INVISIBLE);
    }

    public void hideHasUnread() {
        view_has_unread.setVisibility(INVISIBLE);
    }

    public void hideHasNewCircle() {
        view_has_circle_info.setVisibility(INVISIBLE);
    }

    public void showHasFriend() {
        view_has_new_friend.setVisibility(VISIBLE);
    }

    public void hideHasFriend() {
        view_has_new_friend.setVisibility(INVISIBLE);
    }

    public interface ActiveFlagListener {
        void readMineCircle(int unread);

        void readFriendCircleInfo(boolean isShow);

        void readNewFriendInfo(boolean isShow);
    }
}
